﻿using Base.Entities.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

namespace System
{
    public static class EnumHelper
    {
        /// <summary>
        /// string 转化为 枚举对象
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="value">待转化的string</param>
        /// <returns>T.</returns>
        public static T ToEnum<T>(this int value, T defaultValue = default(T)) where T : struct
        {
            T result = default(T);
            Type typeFromHandle = typeof(T);
            if (!typeFromHandle.IsEnum)
            {
                result = defaultValue;
            }
            if (!Enum.TryParse(value.ToString(), out result))
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// string 转化为 枚举对象
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="value">待转化的string</param>
        /// <returns>T.</returns>
        public static T ToEnum<T>(this string value, T defaultValue = default(T)) where T : struct
        {
            T result;
            Type typeFromHandle = typeof(T);
            if (!typeFromHandle.IsEnum || value.IsNullOrWhiteSpace())
            {
                result = defaultValue;
            }
            else
            {
                if (!Enum.TryParse(value, out result))
                {
                    result = defaultValue;
                }
            }
            return result;
        }

        /// <summary>
        /// 获取说明
        /// </summary>
        /// <param name="e1"></param>
        /// <returns></returns>
        public static string GetDescription(this Enum e1)
        {
            try
            {
                if (e1.GetType().IsEnum)
                {
                    string value = e1.ToString();
                    FieldInfo field = e1.GetType().GetField(value);
                    //获取描述属性
                    object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    //当描述属性没有时，直接返回名称
                    if (objs == null || objs.Length == 0)
                        return value;
                    DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
                    return descriptionAttribute.Description;
                }
            }
            catch (Exception ex)
            {
                GlobalManager.Logger.Log(ex);
            }
            return string.Empty;
        }

        /// <summary>
        /// 对象转为集合
        /// </summary>
        /// <param name="obj">The object.</param>
        public static List<EnumItem<T>> EnumToList<T>()
        {
            List<EnumItem<T>> list = new List<EnumItem<T>>();
            try
            {
                Type EnumType = typeof(T);
                if (!EnumType.IsEnum)
                    return list;
                var fields = EnumType.GetFields(BindingFlags.Static | BindingFlags.Public);
                foreach (var field in fields)
                {
                    //获取描述属性
                    object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    var val = (T)field.GetValue(null);

                    //当描述属性没有时，直接返回名称
                    var tempDescription = val.ToString();

                    if (objs != null && objs.Length != 0)
                    {
                        DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
                        tempDescription = descriptionAttribute.Description;
                    }

                    EnumItem<T> tempEnumItem = new EnumItem<T>(val, tempDescription);
                    list.Add(tempEnumItem);
                }
                return list;
            }
            catch (Exception ex)
            {
                GlobalManager.Logger.Log(ex);
            }
            return list;
        }
    }
}
